de-duplicate format and filter vector display code. (#1068)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Fri, 14 Apr 2023 13:57:20 +0000 (07:57 -0600)
committerGitHub <noreply@github.com>
Fri, 14 Apr 2023 13:57:20 +0000 (07:57 -0600)
* de-duplicate format and filter vector display code.

* consolidate release of argument memory in vecs, filter_vecs.

filter_vecs.cc
filter_vecs.h
main.cc
vecs.cc
vecs.h

index 00308b71c19714fb23f4156370fec392a8c4e6c1..6e0f23d674ef72cc7e49ff887b1229d9d4171ef2 100644 (file)
@@ -262,19 +262,9 @@ FilterVecs::fltinfo_t FilterVecs::find_filter_vec(const QString& fltargstring)
   return {};
 }
 
-void FilterVecs::free_filter_vec(fltinfo_t& filter)
+void FilterVecs::free_filter_vec(Filter* flt)
 {
-  QVector<arglist_t>* args = filter->get_args();
-
-  if (args && !args->isEmpty()) {
-    assert(args->isDetached());
-    for (auto& arg : *args) {
-      if (arg.argvalptr) {
-        xfree(arg.argvalptr);
-        arg.argvalptr = *arg.argval = nullptr;
-      }
-    }
-  }
+  Vecs::free_options(flt->get_args());
 }
 
 void FilterVecs::init_filter_vec(Filter* flt)
@@ -300,16 +290,7 @@ void FilterVecs::init_filter_vecs()
 void FilterVecs::exit_filter_vec(Filter* flt)
 {
     (flt->exit)();
-    QVector<arglist_t>* args = flt->get_args();
-    if (args && !args->isEmpty()) {
-      assert(args->isDetached());
-      for (auto& arg : *args) {
-        if (arg.argvalptr) {
-          xfree(arg.argvalptr);
-          *arg.argval = arg.argvalptr = nullptr;
-        }
-      }
-    }
+    Vecs::free_options(flt->get_args());
 }
 
 void FilterVecs::exit_filter_vecs()
@@ -325,32 +306,14 @@ void FilterVecs::exit_filter_vecs()
  *  Display the available formats in a format that's easy for humans to
  *  parse for help on available command line options.
  */
-void FilterVecs::disp_filter_vecs() const
-{
-  for (const auto& vec : d_ptr_->filter_vec_list) {
-    Filter* flt = (vec.factory != nullptr)? vec.factory() : vec.vec;
-    printf("   %-20.20s  %-50.50s\n",
-           qPrintable(vec.name), qPrintable(vec.desc));
-    const QVector<arglist_t>* args = flt->get_args();
-    if (args) {
-      for (const auto& arg : *args) {
-        if (!(arg.argtype & ARGTYPE_HIDDEN)) {
-          printf("       %-18.18s    %-.50s %s\n",
-                 qPrintable(arg.argstring), qPrintable(arg.helpstring),
-                 (arg.argtype & ARGTYPE_REQUIRED) ? "(required)" : "");
-        }
-      }
-    }
-    if (vec.factory != nullptr) {
-      delete flt;
-    }
-  }
-}
-
 void FilterVecs::disp_filter_vec(const QString& vecname) const
 {
   for (const auto& vec : d_ptr_->filter_vec_list) {
-    if (vecname.compare(vec.name, Qt::CaseInsensitive) != 0) {
+    /*
+     * Display info for all filter is vecname is empty,
+     * otherwise just display info for the matching filter.
+     */
+    if (!vecname.isEmpty() && (vecname.compare(vec.name, Qt::CaseInsensitive) != 0)) {
       continue;
     }
     Filter* flt = (vec.factory != nullptr)? vec.factory() : vec.vec;
index aa5323b66308273b7c38fb2c0fb916353ae9c007..2abe2d36680d48740707b03756bd05fc7417d46a 100644 (file)
@@ -67,13 +67,12 @@ public:
 
   static void prepare_filter(const fltinfo_t& fltdata);
   fltinfo_t find_filter_vec(const QString& fltargstring);
-  static void free_filter_vec(fltinfo_t& filter);
+  static void free_filter_vec(Filter* flt);
   static void init_filter_vec(Filter* flt);
   void init_filter_vecs();
   static void exit_filter_vec(Filter* flt);
   void exit_filter_vecs();
-  void disp_filter_vecs() const;
-  void disp_filter_vec(const QString& vecname) const;
+  void disp_filter_vec(const QString& vecname = QString()) const;
   void disp_filters(int version) const;
   bool validate_filters() const;
 
diff --git a/main.cc b/main.cc
index d3a65301c767d300895a6c304fc1e6dedb782c52..6487ba60430849b06507d48e8067acabb9a9b85a 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -109,7 +109,7 @@ load_args(const QString& filename, const QString& arg0)
 }
 
 static void
-usage(const char* pname, int shorter)
+usage(const char* pname, bool verbose)
 {
   printf("GPSBabel Version %s.  https://www.gpsbabel.org\n\n",
          gpsbabel_version);
@@ -149,14 +149,14 @@ usage(const char* pname, int shorter)
     , pname
     , global_opts.debug_level
   );
-  if (shorter) {
+  if (!verbose) {
     printf("\n\n[Press enter]");
     fgetc(stdin);
   } else {
     printf("File Types (-i and -o options):\n");
-    Vecs::Instance().disp_vecs();
+    Vecs::Instance().disp_vec();
     printf("\nSupported data filters:\n");
-    FilterVecs::Instance().disp_filter_vecs();
+    FilterVecs::Instance().disp_filter_vec();
   }
 }
 
@@ -314,7 +314,7 @@ run(const char* prog_name)
   QStringList qargs = QCoreApplication::arguments();
 
   if (qargs.size() < 2) {
-    usage(prog_name,1);
+    usage(prog_name, false);
     return 0;
   }
 
@@ -346,7 +346,7 @@ run(const char* prog_name)
       if (argn < qargs.size()-1) {
         spec_usage(qargs.at(argn+1));
       } else {
-        usage(prog_name,0);
+        usage(prog_name, true);
       }
       return 0;
     }
@@ -459,7 +459,7 @@ run(const char* prog_name)
           filter->init();
           filter->process();
           filter->deinit();
-          FilterVecs::free_filter_vec(filter);
+          FilterVecs::free_filter_vec(filter.flt);
 
           FilterVecs::exit_filter_vec(filter.flt);
           delete filter.flt;
@@ -469,7 +469,7 @@ run(const char* prog_name)
           filter->init();
           filter->process();
           filter->deinit();
-          FilterVecs::free_filter_vec(filter);
+          FilterVecs::free_filter_vec(filter.flt);
         }
       }  else {
         fatal("Unknown filter '%s'\n",qPrintable(argument));
@@ -541,7 +541,7 @@ run(const char* prog_name)
       return 0;
     case 'h':
     case '?':
-      usage(prog_name,0);
+      usage(prog_name, true);
       return 0;
     case 'p':
       argument = FETCH_OPTARG;
@@ -602,7 +602,7 @@ run(const char* prog_name)
 
     }
   } else if (!qargs.isEmpty()) {
-    usage(prog_name,0);
+    usage(prog_name, true);
     return 0;
   }
   if (!ovecs) {
diff --git a/vecs.cc b/vecs.cc
index d4a6fb0d1fb34408f5de6e8b763cfe8f37539d9d..24e36f474ecfeeb4d114c59cda86c0185436854c 100644 (file)
--- a/vecs.cc
+++ b/vecs.cc
@@ -660,10 +660,8 @@ bool Vecs::is_bool(const QString& val)
          (!val.isEmpty() && val.at(0).isDigit());
 }
 
-void Vecs::exit_vec(Format* fmt)
+void Vecs::free_options(QVector<arglist_t>* args)
 {
-  (fmt->exit)();
-  QVector<arglist_t>* args = fmt->get_args();
   if (args && !args->isEmpty()) {
     assert(args->isDetached());
     for (auto& arg : *args) {
@@ -675,6 +673,12 @@ void Vecs::exit_vec(Format* fmt)
   }
 }
 
+void Vecs::exit_vec(Format* fmt)
+{
+  (fmt->exit)();
+  free_options(fmt->get_args());
+}
+
 void Vecs::exit_vecs()
 {
   for (const auto& vec : d_ptr_->vec_list) {
@@ -1040,39 +1044,20 @@ QVector<Vecs::vecinfo_t> Vecs::sort_and_unify_vecs() const
   return svp;
 }
 
-#define VEC_FMT "      %-20.20s  %-.50s\n"
-
-void Vecs::disp_vecs() const
-{
-  const auto svp = sort_and_unify_vecs();
-  for (const auto& vec : svp) {
-    if (vec.type == ff_type_internal)  {
-      continue;
-    }
-    printf(VEC_FMT, qPrintable(vec.name), qPrintable(vec.desc));
-    const QVector<arginfo_t> args = vec.arginfo;
-    for (const auto& arg : args) {
-      if (!(arg.argtype & ARGTYPE_HIDDEN)) {
-        printf("         %-18.18s    %s%-.50s%s\n",
-               qPrintable(arg.argstring),
-               (arg.argtype & ARGTYPE_TYPEMASK) ==
-               ARGTYPE_BOOL ? "(0/1) " : "",
-               qPrintable(arg.helpstring),
-               (arg.argtype & ARGTYPE_REQUIRED) ? " (required)" : "");
-      }
-    }
-  }
-}
-
 void Vecs::disp_vec(const QString& vecname) const
 {
   const auto svp = sort_and_unify_vecs();
   for (const auto& vec : svp) {
-    if (vecname.compare(vec.name, Qt::CaseInsensitive) != 0)  {
+    /*
+     * Display info for all non-internal formats is vecname is empty,
+     * otherwise just display info for the matching format.
+     */
+    if (vecname.isEmpty()? (vec.type == ff_type_internal) :
+        (vecname.compare(vec.name, Qt::CaseInsensitive) != 0)) {
       continue;
     }
 
-    printf(VEC_FMT, qPrintable(vec.name), qPrintable(vec.desc));
+    printf(" %-20.20s  %-.50s\n", qPrintable(vec.name), qPrintable(vec.desc));
     const QVector<arginfo_t> args = vec.arginfo;
     for (const auto& arg : args) {
       if (!(arg.argtype & ARGTYPE_HIDDEN)) {
diff --git a/vecs.h b/vecs.h
index 9c585d4322042eb7166302c7a6372cf3e48b1180..e2ccf48298f2b6a232bb4ae8c09d2d3d312026af 100644 (file)
--- a/vecs.h
+++ b/vecs.h
@@ -73,6 +73,7 @@ public:
 
   static void init_vec(Format* fmt);
   void init_vecs();
+  static void free_options(QVector<arglist_t>* args);
   static void exit_vec(Format* fmt);
   void exit_vecs();
   static void assign_option(const QString& module, arglist_t* arg, const QString& val);
@@ -81,8 +82,7 @@ public:
   static QString get_option(const QStringList& options, const QString& argname);
   static void prepare_format(const fmtinfo_t& data);
   fmtinfo_t find_vec(const QString& fmtargstring);
-  void disp_vecs() const;
-  void disp_vec(const QString& vecname) const;
+  void disp_vec(const QString& vecname = QString()) const;
   static const char* name_option(uint32_t type);
   void disp_formats(int version) const;
   static bool validate_args(const QString& name, const QVector<arglist_t>* args);